package day22.collection;

import java.util.*;

/**
 * Set接口方法测试类
 * 涵盖Set接口的所有基础方法的详细测试
 */
public class SetMethodTest {
    public static void main(String[] args) {
        System.out.println("=== Set接口方法详细测试 ===\n");
        
        // 1. 测试HashSet实现
        System.out.println("1. 测试HashSet实现:");
        Set<String> hashSet = new HashSet<>();
        
        // add(E e) - 添加元素
        System.out.println("   测试add(E e)方法:");
        hashSet.add("apple");
        hashSet.add("banana");
        hashSet.add("orange");
        hashSet.add("apple"); // 重复元素
        System.out.println("      添加元素后: " + hashSet);
        System.out.println("      HashSet不保证顺序，且自动去重");
        
        // size() - 返回元素个数
        System.out.println("   测试size()方法:");
        System.out.println("      元素个数: " + hashSet.size());
        
        // isEmpty() - 判断是否为空
        System.out.println("   测试isEmpty()方法:");
        System.out.println("      是否为空: " + hashSet.isEmpty());
        
        // contains(Object o) - 判断是否包含指定元素
        System.out.println("   测试contains(Object o)方法:");
        System.out.println("      是否包含banana: " + hashSet.contains("banana"));
        System.out.println("      是否包含grape: " + hashSet.contains("grape"));
        
        // remove(Object o) - 移除指定元素
        System.out.println("   测试remove(Object o)方法:");
        boolean removed = hashSet.remove("banana");
        System.out.println("      移除banana: " + removed);
        System.out.println("      移除后: " + hashSet);
        
        boolean notRemoved = hashSet.remove("banana");
        System.out.println("      再次移除不存在的banana: " + notRemoved);
        
        // toArray() - 转换为数组
        System.out.println("   测试toArray()方法:");
        Object[] array = hashSet.toArray();
        System.out.println("      转换为Object数组: " + Arrays.toString(array));
        
        // toArray(T[] a) - 转换为指定类型数组
        String[] stringArray = hashSet.toArray(new String[0]);
        System.out.println("      转换为String数组: " + Arrays.toString(stringArray));
        
        // iterator() - 获取迭代器
        System.out.println("   测试iterator()方法:");
        Iterator<String> iterator = hashSet.iterator();
        System.out.print("      迭代器遍历: ");
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
        
        // clear() - 清空所有元素
        System.out.println("   测试clear()方法:");
        hashSet.clear();
        System.out.println("      清空后: " + hashSet);
        System.out.println("      清空后元素个数: " + hashSet.size());
        
        // 2. 测试LinkedHashSet实现
        System.out.println("\n2. 测试LinkedHashSet实现:");
        Set<String> linkedHashSet = new LinkedHashSet<>();
        
        linkedHashSet.add("apple");
        linkedHashSet.add("banana");
        linkedHashSet.add("orange");
        linkedHashSet.add("grape");
        System.out.println("   添加元素后: " + linkedHashSet);
        System.out.println("   LinkedHashSet保持插入顺序");
        
        // 3. 测试TreeSet实现
        System.out.println("\n3. 测试TreeSet实现:");
        Set<String> treeSet = new TreeSet<>();
        
        treeSet.add("banana");
        treeSet.add("apple");
        treeSet.add("orange");
        treeSet.add("grape");
        System.out.println("   添加元素后: " + treeSet);
        System.out.println("   TreeSet按自然排序(字母顺序)");
        
        // TreeSet特有方法
        TreeSet<String> treeSet2 = new TreeSet<>();
        treeSet2.add("banana");
        treeSet2.add("apple");
        treeSet2.add("orange");
        treeSet2.add("grape");
        treeSet2.add("cherry");
        
        System.out.println("   TreeSet特有方法测试:");
        System.out.println("      TreeSet内容: " + treeSet2);
        System.out.println("      first()获取第一个元素: " + treeSet2.first());
        System.out.println("      last()获取最后一个元素: " + treeSet2.last());
        System.out.println("      lower(\"grape\")获取小于grape的最高元素: " + treeSet2.lower("grape"));
        System.out.println("      higher(\"grape\")获取大于grape的最低元素: " + treeSet2.higher("grape"));
        System.out.println("      floor(\"grape\")获取小于等于grape的最高元素: " + treeSet2.floor("grape"));
        System.out.println("      ceiling(\"grape\")获取大于等于grape的最低元素: " + treeSet2.ceiling("grape"));
        
        // subSet(E fromElement, E toElement) - 获取子集
        System.out.println("      subSet(\"banana\", \"orange\"): " + treeSet2.subSet("banana", "orange"));
        
        // headSet(E toElement) - 获取指定元素之前的子集
        System.out.println("      headSet(\"grape\"): " + treeSet2.headSet("grape"));
        
        // tailSet(E fromElement) - 获取指定元素之后的子集
        System.out.println("      tailSet(\"grape\"): " + treeSet2.tailSet("grape"));
        
        // descendingIterator() - 降序迭代器
        System.out.println("      降序遍历: ");
        Iterator<String> descendingIterator = treeSet2.descendingIterator();
        System.out.print("         ");
        while (descendingIterator.hasNext()) {
            System.out.print(descendingIterator.next() + " ");
        }
        System.out.println();
        
        // descendingIterator() - 降序迭代器
        System.out.println("      descendingIterator()降序遍历:");
        Iterator<String> descIter = treeSet2.descendingIterator();
        System.out.print("         ");
        while (descIter.hasNext()) {
            System.out.print(descIter.next() + " ");
        }
        System.out.println();
        
        // 4. 测试Set集合操作
        System.out.println("\n4. 测试Set集合操作:");
        
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        
        set1.add(1);
        set1.add(2);
        set1.add(3);
        set1.add(4);
        
        set2.add(3);
        set2.add(4);
        set2.add(5);
        set2.add(6);
        
        System.out.println("   set1: " + set1);
        System.out.println("   set2: " + set2);
        
        // removeAll(Collection<?> c) - 差集
        Set<Integer> difference = new HashSet<>(set1);
        difference.removeAll(set2);
        System.out.println("   set1 - set2 差集: " + difference);
        
        // retainAll(Collection<?> c) - 交集
        Set<Integer> intersection = new HashSet<>(set1);
        intersection.retainAll(set2);
        System.out.println("   set1 ∩ set2 交集: " + intersection);
        
        // addAll(Collection<? extends E> c) - 并集
        Set<Integer> union = new HashSet<>(set1);
        union.addAll(set2);
        System.out.println("   set1 ∪ set2 并集: " + union);
        
        // containsAll(Collection<?> c) - 包含所有元素
        Set<Integer> set3 = new HashSet<>();
        set3.add(1);
        set3.add(2);
        
        System.out.println("   set3: " + set3);
        System.out.println("   set1是否包含set3的所有元素: " + set1.containsAll(set3));
        System.out.println("   set1是否包含set2的所有元素: " + set1.containsAll(set2));
        
        System.out.println("\n=== Set接口方法测试完成 ===");
    }
}